package dcp.bss.modules.bussiness.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.List;

import dcp.bss.common.util.PageUtils;
import dcp.bss.common.util.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import dcp.bss.query.QueryViewVo;
import dcp.bss.web.util.ExcelUtil;
import dcp.bss.modules.bussiness.entity.AlarmEntity;
import dcp.bss.modules.bussiness.service.AlarmService;


/**
 * 报警表
 *
 * @author caochaofeng
 * @email caochaofeng@dacanginfo.com
 * @date 2018-08-19 17:21:27
 */
@RestController
@RequestMapping("alarm")
@Slf4j
public class AlarmController {
    @Autowired
    private AlarmService alarmService;

    /**
     * 列表
     */
    @RequestMapping("/page")
    // @RequiresPermissions("bussiness:alarm:list")
    public R list(QueryViewVo<AlarmEntity> queryViewVo) {
        PageUtils page = alarmService.queryPage(queryViewVo);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("bussiness:alarm:info")
    public R info(@PathVariable("id") Long id) {
        AlarmEntity alarm = alarmService.selectById(id);

        return R.ok().put("alarm", alarm);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("bussiness:alarm:save")
    public R save(@RequestBody AlarmEntity alarm) {
        alarmService.insert(alarm);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("bussiness:alarm:update")
    public R update(@RequestBody AlarmEntity alarm) {
        alarmService.updateById(alarm);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("bussiness:alarm:delete")
    public R delete(@RequestBody Long[] ids) {
        alarmService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 导出数据到excel
     *
     * @param response
     * @param queryViewVo
     * @return
     */
    @RequestMapping("/download")
    //  @RequiresPermissions("bussiness:alarm:download")
    public void download(HttpServletResponse response, QueryViewVo<AlarmEntity> queryViewVo) {
        try {
            List<AlarmEntity> download = alarmService.download(queryViewVo);
            XSSFWorkbook sfworkbook = new XSSFWorkbook();
            ExcelUtil excelUtil = new ExcelUtil();
            SXSSFWorkbook createWorkbook = excelUtil.createWorkbook(sfworkbook);
            ExcelUtil.WriteExcel<AlarmEntity> writeexcel = new ExcelUtil.WriteExcel<AlarmEntity>() {

                @Override
                public void writeHead(XSSFRow contentxfrow) {
                    XSSFCell createCell1 = contentxfrow.createCell(0);
                    createCell1.setCellValue("报警id");
                    XSSFCell createCell2 = contentxfrow.createCell(1);
                    createCell2.setCellValue("报警编码");
                    XSSFCell createCell3 = contentxfrow.createCell(2);
                    createCell3.setCellValue("运输企业主键");
                    XSSFCell createCell4 = contentxfrow.createCell(3);
                    createCell4.setCellValue("运输企业名称");
                    XSSFCell createCell5 = contentxfrow.createCell(4);
                    createCell5.setCellValue("牵引车号");
                    XSSFCell createCell6 = contentxfrow.createCell(5);
                    createCell6.setCellValue("牵引车主键");
                    XSSFCell createCell7 = contentxfrow.createCell(6);
                    createCell7.setCellValue("挂车号");
                    XSSFCell createCell8 = contentxfrow.createCell(7);
                    createCell8.setCellValue("挂车主键");
                    XSSFCell createCell9 = contentxfrow.createCell(8);
                    createCell9.setCellValue("驾驶员名称");
                    XSSFCell createCell10 = contentxfrow.createCell(9);
                    createCell10.setCellValue("驾驶员关联的pers表的主键");
                    XSSFCell createCell11 = contentxfrow.createCell(10);
                    createCell11.setCellValue("押运员名称");
                    XSSFCell createCell12 = contentxfrow.createCell(11);
                    createCell12.setCellValue("押运员关联的pers表的主键");
                    XSSFCell createCell13 = contentxfrow.createCell(12);
                    createCell13.setCellValue("报警时间");
                    XSSFCell createCell14 = contentxfrow.createCell(13);
                    createCell14.setCellValue("报警点地址");
                    XSSFCell createCell15 = contentxfrow.createCell(14);
                    createCell15.setCellValue("报警点经度");
                    XSSFCell createCell16 = contentxfrow.createCell(15);
                    createCell16.setCellValue("报警点纬度");
                    XSSFCell createCell17 = contentxfrow.createCell(16);
                    createCell17.setCellValue("报警类型cd");
                    XSSFCell createCell18 = contentxfrow.createCell(17);
                    createCell18.setCellValue("报警类型");
                    XSSFCell createCell19 = contentxfrow.createCell(18);
                    createCell19.setCellValue("报警详情");
                    XSSFCell createCell20 = contentxfrow.createCell(19);
                    createCell20.setCellValue("是否处理，1表示已处理，0表示未处理");
                    XSSFCell createCell21 = contentxfrow.createCell(20);
                    createCell21.setCellValue("处理人");
                    XSSFCell createCell22 = contentxfrow.createCell(21);
                    createCell22.setCellValue("处理人pk");
                    XSSFCell createCell23 = contentxfrow.createCell(22);
                    createCell23.setCellValue("报警值");
                    XSSFCell createCell24 = contentxfrow.createCell(23);
                    createCell24.setCellValue("报警阀值");
                    XSSFCell createCell25 = contentxfrow.createCell(24);
                    createCell25.setCellValue("创建时间");
                    XSSFCell createCell26 = contentxfrow.createCell(25);
                    createCell26.setCellValue("创建者");
                    XSSFCell createCell27 = contentxfrow.createCell(26);
                    createCell27.setCellValue("更新者");
                    XSSFCell createCell28 = contentxfrow.createCell(27);
                    createCell28.setCellValue("更新时间");
                    XSSFCell createCell29 = contentxfrow.createCell(28);
                    createCell29.setCellValue("");
                }

                @Override
                public void write(XSSFRow contentxfrow, AlarmEntity t) {
                    XSSFCell createCell1 = contentxfrow.createCell(0);
                    createCell1.setCellValue(t.getId());
                    XSSFCell createCell2 = contentxfrow.createCell(1);
                    createCell2.setCellValue(t.getCd());
                    XSSFCell createCell3 = contentxfrow.createCell(2);
                    createCell3.setCellValue(t.getEntpPk());
                    XSSFCell createCell4 = contentxfrow.createCell(3);
                    createCell4.setCellValue(t.getEntpName());
                    XSSFCell createCell5 = contentxfrow.createCell(4);
                    createCell5.setCellValue(t.getVecNo());
                    XSSFCell createCell6 = contentxfrow.createCell(5);
                    createCell6.setCellValue(t.getVecPk());
                    XSSFCell createCell7 = contentxfrow.createCell(6);
                    createCell7.setCellValue(t.getTrailerNo());
                    XSSFCell createCell8 = contentxfrow.createCell(7);
                    createCell8.setCellValue(t.getTrailerPk());
                    XSSFCell createCell9 = contentxfrow.createCell(8);
                    createCell9.setCellValue(t.getDriverName());
                    XSSFCell createCell10 = contentxfrow.createCell(9);
                    createCell10.setCellValue(t.getDriverPk());
                    XSSFCell createCell11 = contentxfrow.createCell(10);
                    createCell11.setCellValue(t.getScName());
                    XSSFCell createCell12 = contentxfrow.createCell(11);
                    createCell12.setCellValue(t.getScPk());
                    XSSFCell createCell13 = contentxfrow.createCell(12);
                    createCell13.setCellValue(t.getAlarmTime());
                    XSSFCell createCell14 = contentxfrow.createCell(13);
                    createCell14.setCellValue(t.getAlarmLocation());
                    XSSFCell createCell15 = contentxfrow.createCell(14);
                    createCell15.setCellValue(t.getAlarmLongitude());
                    XSSFCell createCell16 = contentxfrow.createCell(15);
                    createCell16.setCellValue(t.getAlarmLatitude());
                    XSSFCell createCell17 = contentxfrow.createCell(16);
                    createCell17.setCellValue(t.getCatCd());
                    XSSFCell createCell18 = contentxfrow.createCell(17);
                    createCell18.setCellValue(t.getCatNmCn());
                    XSSFCell createCell19 = contentxfrow.createCell(18);
                    createCell19.setCellValue(t.getDescr());
                    XSSFCell createCell20 = contentxfrow.createCell(19);
                    createCell20.setCellValue(t.getIsHandle());
                    XSSFCell createCell21 = contentxfrow.createCell(20);
                    createCell21.setCellValue(t.getHandler());
                    XSSFCell createCell22 = contentxfrow.createCell(21);
                    createCell22.setCellValue(t.getHandlerPk());
                    XSSFCell createCell23 = contentxfrow.createCell(22);
                    createCell23.setCellValue(t.getAlarmValue());
                    XSSFCell createCell24 = contentxfrow.createCell(23);
                    createCell24.setCellValue(t.getThreshold());
                    XSSFCell createCell25 = contentxfrow.createCell(24);
                    createCell25.setCellValue(t.getCrtTm());
                    XSSFCell createCell26 = contentxfrow.createCell(25);
                    createCell26.setCellValue(t.getCrtBy());
                    XSSFCell createCell27 = contentxfrow.createCell(26);
                    createCell27.setCellValue(t.getUpdBy());
                    XSSFCell createCell28 = contentxfrow.createCell(27);
                    createCell28.setCellValue(t.getUpdTm());
                    XSSFCell createCell29 = contentxfrow.createCell(28);
                    createCell29.setCellValue(t.getEditFlag());
                }
            };
            excelUtil.createHead(sfworkbook, createWorkbook, writeexcel);
            excelUtil.writeXlsFile(sfworkbook, createWorkbook, download, writeexcel);
            /**
             * 保存文件
             */
            String saveXlsFile = excelUtil.saveXlsFile(sfworkbook);
            excelUtil.sendExcel(response, saveXlsFile);
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
        }
    }


}
